package net.m510.autosurfer.core.log;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.m510.autosurfer.core.utils.AnalyzeUtils;

import org.apache.http.cookie.Cookie;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.FormElement;
import org.jsoup.select.Elements;


public class LogUtils {
	public static void logForms(Logger log,Level level, List<FormElement> forms ) {
		log.log(level,"Total Forms : "+forms.size());
		for (int i=0; i < forms.size(); i++ ) {
			FormElement form = forms.get(i);
			log.log(level,"form["+i+"].attrs:"+form.attributes());
			logElements(log,level, AnalyzeUtils.getFormDataElements(form), "f["+i+"].p");
		}
	}
	public static void logTitle(Logger log, Level level, Document doc) {
		if ( doc == null ) return;
		Elements e = doc.head().getElementsByTag("title");
		if ( e.isEmpty() ) {
			log.log(level,"no title tags");
		} else {
			logElements(log, level, e);
		}
	}
	
	public static void logElements(Logger log, Level level, Elements elems) {
		logElements(log, level, elems,"");
	}
	public static void logElements(Logger log, Level level, Elements elems, String appendMsg) {
		for (int i=0; i < elems.size(); i++ ) {
			Element e = elems.get(i);
			log.log(level,appendMsg+"["+i+"]:"+e.toString());
		}
	}

	public static void logCookies(Logger log, Level level, List<Cookie> cookies, String description) {
		log.log(level,description);
		logCookies(log,level,cookies);
	}
	public static void logCookies(Logger log,Level level, List<Cookie> cookies) {
		if ( cookies.isEmpty() ) {
			log.log(level,"No cookies");
		} else {
			for (int i = 0; i < cookies.size(); i++ ) {
				Cookie cookie = cookies.get(i);
				log.log(level,"cookie["+i+"]="+cookie.toString());
			}
		}
	}
	public static void logStacktrace(Logger log,Level level,StackTraceElement[] traces  ) {
		for (StackTraceElement trace : traces) {
			log.log(level,trace.toString());
		}
	}
}
